package com.chuanyang.driverh.mainmy;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.amap.LocationEntity;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.responseresult.LinesManageData;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.AddAddrEvent;
import com.chuanyang.driverh.utils.eventbus.ReUpdateLinesEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 *
 */

public class AddLinesActivity extends BaseTitleActivity {


    private TextView tv_start;
    private TextView tv_end;


    private LinesManageData.DataBean bean;
    private boolean isUpdata = false;//false添加，true修改
    private LocationEntity locStart;
    private LocationEntity locEnd;
//    private PathEntity pathStart;
//    private PathEntity pathEnd;

    @Override
    protected int getContentResId() {
        return R.layout.my_act_addline;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_start = (TextView) findViewById(R.id.tv_addline_startadd);
        tv_end = (TextView) findViewById(R.id.tv_addline_endadd);
    }
    @Override
    protected void initData() {
        setBackImg();
        if (getIntent().getSerializableExtra("data") != null) {//表示点击路线列表进入修改
            getRightTextView().setText("修改");
            getTitleTextView().setText("修改路线");
            bean = (LinesManageData.DataBean) getIntent().getSerializableExtra("data");
            isUpdata = true;
            tv_start.setText(bean.getStartAddr());
            tv_end.setText(bean.getTargetAddr());
        }else{
            getTitleTextView().setText("添加路线");
            getRightTextView().setText("确定");
            bean=new LinesManageData.DataBean();
            isUpdata = false;
        }
    }

    @Override
    protected void initListener() {
        findViewById(R.id.ll_addline_startadd).setOnClickListener(this);
        findViewById(R.id.ll_addline_endadd).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_addline_startadd:
                LogUtils.d("AddLinesActivity",System.currentTimeMillis()+"");
                ActivityUtil.startActivity(activityContext,new Intent(activityContext,AddAddrActivity.class).putExtra("type",2));
                break;
            case R.id.ll_addline_endadd:
                ActivityUtil.startActivity(activityContext,new Intent(activityContext,AddAddrActivity.class).putExtra("type",3));
                break;
        }
    }

    //选取地址后
    public void onEventMainThread(AddAddrEvent event) {

        if (event.getEntity() != null && event.getType()==2) {
            locStart = event.getEntity();
//           pathStart= ParseUtil.parseLocationToPath(locStart);
//            pathStart.setPointType("1");
//            String sadd = pathStart.getCity() + " " + pathStart.getRegions();
            tv_start.setText(locStart.getCity()+" "+locStart.getAddress());

        }else if(event.getEntity() != null && event.getType()==3 ){
            locEnd = event.getEntity();
//            pathEnd= ParseUtil.parseLocationToPath(locEnd);
//            pathEnd.setPointType("2");
//            String  eadd = pathEnd.getCity() + " " + pathEnd.getRegions();
            tv_end.setText(locEnd.getCity()+" "+locEnd.getAddress());
        }
    }



    @Override
    protected void baseGoEnsure() {
        if (!StringUtil.isBlank(tv_start.getText().toString().trim()) && !StringUtil.isBlank(tv_end.getText().toString().trim())) {
            if (isUpdata) {
                upDataLines();
            } else {
                addLines();
            }
        } else {
            PrintUtil.toast(activityContext,"请填写起止点");
        }
    }



    private void addLines() {
        DialogUtil.showLoadingDialog(activityContext,"添加线路...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("startCity", locStart.getCity());
        params.put("targetCity", locEnd.getCity());
        params.put("startAddr",locStart.getCity()+" "+ locStart.getAddress());
        params.put("targetAddr", locEnd.getCity()+" "+ locEnd.getAddress());
        MyHttpUtil.addLines(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    EventBus.getDefault().post(new ReUpdateLinesEvent(true));
                    PrintUtil.toast(activityContext,"路线添加成功");
                    finish();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }
    private void upDataLines() {




        DialogUtil.showLoadingDialog(activityContext,"添加线路...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", bean.getId() + "");
        params.put("startCity", locStart==null?bean.getStartCity():locStart.getCity());
        params.put("targetCity", locEnd==null?bean.getTargetCity():locEnd.getCity());
        params.put("startAddr",locStart==null?bean.getStartAddr():(locStart.getCity()+" "+ locStart.getAddress()));
        params.put("targetAddr",locEnd==null?bean.getTargetAddr():(locEnd.getCity()+" "+ locEnd.getAddress()));

        MyHttpUtil.upDataLines(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    EventBus.getDefault().post(new ReUpdateLinesEvent(true));
                    PrintUtil.toast(activityContext,"路线修改成功");
                    finish();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }


}
